package com.ruoyi.quartz.task;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.python.PythonScriptExecute;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PythonScript;
import com.ruoyi.system.service.IPythonScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("PythonScriptTask")
public class PythonScriptTask {

    @Autowired
    private PythonScriptExecute pythonScriptExecute;

    @Autowired
    private IPythonScriptService pythonScriptService;

    public void execute(String scriptName) {
        PythonScript pythonScript = pythonScriptService.selectPythonScriptName(scriptName);
        if (pythonScript == null) {
            throw new ServiceException("未查询到脚本配置");
        }
        if (StringUtils.isAnyBlank(pythonScript.getScriptPath(), pythonScript.getScriptName())) {
            throw new ServiceException("未查询到脚本配置");
        }
        boolean execute = pythonScriptExecute.execute(pythonScript.getScriptPath(), pythonScript.getScriptFileName());
        if (!execute) {
            throw new ServiceException("脚本执行失败");
        }
    }
}
